using System;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace Elevation.Common.Services.Helpers
{
	internal sealed class SafeSidHandler : SafeHandleZeroOrMinusOneIsInvalid
	{
		#region Extern

		[DllImport("advapi32.dll", SetLastError = true)]
		private static extern IntPtr FreeSid(IntPtr pSid);

		#endregion Extern

		#region Constructors

		private SafeSidHandler()
			: base(true)
		{
			//
		}

		#endregion Constructors

		#region SafeHandle overrides

		protected override bool ReleaseHandle()
		{
			try
			{
				var result = FreeSid(handle);
				var sucess = result == IntPtr.Zero;

				return sucess;
			}
			catch
			{
				return false;
			}
		}

		#endregion SafeHandle overrides
	}
}